{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Iterrows 🤜 🤛  Itertuples "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading from a csv\n",
    "df_penguins = pd.read_csv('penguins.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "_______________________________\n",
      "species                 Adelie\n",
      "island               Torgersen\n",
      "bill_length_mm            39.1\n",
      "bill_depth_mm             18.7\n",
      "flipper_length_mm        181.0\n",
      "body_mass_g             3750.0\n",
      "sex                       Male\n",
      "Name: 0, dtype: object\n",
      "__________time taken___________\n",
      "CPU times: user 12.6 ms, sys: 1.29 ms, total: 13.9 ms\n",
      "Wall time: 16 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# 🔴 iterrows: Iterate over DataFrame rows as (index, Series) pairs.\n",
    "index_series_pairs = [tup for tup in df_penguins.iterrows()]\n",
    "idx, series = index_series_pairs[0]\n",
    "print(idx)\n",
    "print('_______________________________')\n",
    "print(series)\n",
    "print('__________time taken___________')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pandas(Index=0, species='Adelie', island='Torgersen', bill_length_mm=39.1, bill_depth_mm=18.7, flipper_length_mm=181.0, body_mass_g=3750.0, sex='Male')\n",
      "__________time taken___________\n",
      "CPU times: user 1.36 ms, sys: 341 µs, total: 1.7 ms\n",
      "Wall time: 1.48 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# 🟡 itertuples: Iterate over DataFrame rows as namedtuples.\n",
    "# Wondering what a namedtuple is❓ I have added a tweet👇 in the thread 🧵 , don't worry 🙌\n",
    "named_tuples = [tup for tup in df_penguins.itertuples()]\n",
    "print(named_tuples[0])\n",
    "print('__________time taken___________')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Concluding remarks 👇 \n",
    "##### Although, it's an anti pattern to iterate over dataframe rows in this manner\n",
    "##### (We will see better techniques in future post, stay tuned!)\n",
    "##### 👉 But, given the choice itertuples is way fater 🏎💨 than iterrows because iterrows has to typecast each\n",
    "##### row into a pandas Series, which is an overhead.\n",
    "#### ❇️ Cheers!! 🍻 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
